package cn.italobing.mybatis.text;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import cn.itlaobing.entity.Student;
import cn.itlaobing.mapper.StudentMapper;

public class StudentMapperText extends BaseMapperText{

	@Test
	public void testFindById() throws IOException {
		SqlSession sqlSession=sqlSession();
		
		try {
			StudentMapper studentMapper =sqlSession.getMapper(StudentMapper.class);
			Student student =studentMapper.selectByPrimaryKey(11);
			
			log.info(Arrays.toString(student.getFavourite()));
		} finally {
			sqlSession.close();
		}
	}
	
	@Test
	public void testInsert() throws IOException {
		SqlSession sqlSession=sqlSession();
		
		try {
			StudentMapper studentMapper =sqlSession.getMapper(StudentMapper.class);
			Student student =new Student();
			student.setName("aaa");
			student.setBirthday(new Date());
			student.setFavourite(new String[]{"吃","喝","玩"});
			studentMapper.insert(student);
			sqlSession.commit();
			log.info(student.getId());
			
		} finally {
			sqlSession.close();
		}
	}
	
	@Test
	public void testUpdateByPrimaryKey() throws IOException {
		SqlSession sqlSession=sqlSession();
		
		try {
			StudentMapper studentMapper =sqlSession.getMapper(StudentMapper.class);
			Student student =new Student();
			student.setId(10);
			student.setName("aaa");
			student.setBirthday(new Date());
			studentMapper.updateByPrimaryKey(student);
			sqlSession.commit();
			log.info(student.getName());
			
		} finally {
			sqlSession.close();
		}
	}
	
	@Test
	public void testUpdateByPrimaryKeySelective() throws IOException {
		SqlSession sqlSession=sqlSession();
		
		try {
			StudentMapper studentMapper =sqlSession.getMapper(StudentMapper.class);
			Student student =new Student();
			student.setId(10);
			student.setName("aaa");
			student.setBirthday(new Date());
			studentMapper.updateByPrimaryKeySelective(student);
			sqlSession.commit();
			log.info(student.getName());
			
		} finally {
			sqlSession.close();
		}
	}
}
 